package com.scirillo.frases.ui;

import java.util.Calendar;

import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.scirillo.frases.R;
import com.scirillo.frases.preference.FrasesPreferenceManager;
import com.scirillo.frases.util.Constants;

public class Settings extends SherlockActivity{
	
	private TextView textViewTime;
    private TimePicker timePicker;
    private RelativeLayout mShowAllFrases;
    private int hour;
    private int minute;
    static final int TIME_DIALOG_ID = 999;
    static boolean updateFragments = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
	    com.actionbarsherlock.app.ActionBar actionBar = getSupportActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);	   
		setLayoutComponents();
		setCurrentTimeOnView();
	}
	
	private void setLayoutComponents() {
		RelativeLayout time = (RelativeLayout) findViewById(R.id.time);
		mShowAllFrases = (RelativeLayout) findViewById(R.id.show_all_frases);
		final CheckBox cbxShowAll = (CheckBox) findViewById(R.id.CheckBox01);
		cbxShowAll.setChecked(FrasesPreferenceManager.GetPTPreferencesBool(Constants.SHOW_ALL_FRASES, false));
		mShowAllFrases.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				cbxShowAll.performClick();				
				updateFragments = cbxShowAll.isChecked();
				FrasesPreferenceManager.SetPTPreferences(Constants.SHOW_ALL_FRASES, cbxShowAll.isChecked());				
			}
		});
		cbxShowAll.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				FrasesPreferenceManager.SetPTPreferences(Constants.SHOW_ALL_FRASES, isChecked);				
			}
		});
		
		time.setOnClickListener(new OnClickListener() {			
			@SuppressWarnings("deprecation")
			@Override
			public void onClick(View v) {
			
				 showDialog(TIME_DIALOG_ID);
				
				}
			});
		
		
		}			  	     
	
       public void setCurrentTimeOnView() {
           textViewTime = new TextView(getApplicationContext());
           timePicker = new TimePicker(getApplicationContext());
           final Calendar c = Calendar.getInstance();
           hour = c.get(Calendar.HOUR_OF_DAY);
           minute = c.get(Calendar.MINUTE);
           textViewTime.setText(new StringBuilder().append(padding_str(hour)).append(":").append(padding_str(minute)));
           timePicker.setCurrentHour(hour);
           timePicker.setCurrentMinute(minute);
       }
       
       @Override
       protected Dialog onCreateDialog(int id) {
           switch (id) {
           case TIME_DIALOG_ID:
               return new TimePickerDialog(this, timePickerListener, hour, minute,false);
           }
           return null;
       }
       private TimePickerDialog.OnTimeSetListener timePickerListener =  new TimePickerDialog.OnTimeSetListener() {
           public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute) {
               hour = selectedHour;            
               minute = selectedMinute;
               textViewTime.setText(new StringBuilder().append(padding_str(hour)).append(":").append(padding_str(minute)));
               timePicker.setCurrentHour(hour);
               timePicker.setCurrentMinute(minute);
               FrasesPreferenceManager.SetPTPreferences(Constants.NOTIFICATION_TIME_HS, hour);
               FrasesPreferenceManager.SetPTPreferences(Constants.NOTIFICATION_TIME_MIN, minute);
           }

       };
       private static String padding_str(int c) {
             if (c >= 10)

              return String.valueOf(c);

           else

              return "0" + String.valueOf(c);

       }


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Settings.this.finish();
		return super.onOptionsItemSelected(item);
	}

}
